package com.dymedia.aibo.mvp.ui.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.dymedia.aibo.R;

import timber.log.Timber;


public class VideoPlayerView extends LinearLayout {
    MyVideoView mMyVideoView;
    ImageView imageButton_play;
    SeekBar seekbar;
    TextView textview_current;
    TextView textview_duration;
    Handler mHandler = new Handler();
    TextView textview_title;

    public VideoPlayerView(Context context) {
        super(context);
        init();
    }

    public VideoPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public void initVideoView(MyVideoView videoview) {
        mMyVideoView = videoview;
    }

    private void init() {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.widget_videoplayer, this);
        textview_title = rootView.findViewById(R.id.textview_title);
        imageButton_play = rootView.findViewById(R.id.imageButton_play);
        seekbar = rootView.findViewById(R.id.seekbar);
        textview_current = rootView.findViewById(R.id.textview_current);
        textview_duration = rootView.findViewById(R.id.textview_duration);
        imageButton_play.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMyVideoView.isPlaying()) {
                    mMyVideoView.pause();
                    imageButton_play.setImageResource( android.R.drawable.ic_media_play);
                } else {
                    mMyVideoView.start();
                    imageButton_play.setImageResource( android.R.drawable.ic_media_pause);
                }
            }
        });
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textview_current.setText(timeFormat(progress * getDuration() / maxprogress / 1000));
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                mHandler.removeCallbacksAndMessages(null);
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                seekbar.setProgress(seekBar.getProgress());
                seek();
            }
        });
    }
    private int getDuration(){
        int duration = mMyVideoView.getDuration();
        if (duration > 0)
            return duration;
        return 1000000;
    }

    public void setTitle(String title) {
        textview_title.setText(title);
    }

    public boolean isShow() {
        return getVisibility() == View.VISIBLE;
    }

    public void show(){
        show(-1);
    }
    public void show(int progress_in) {
        mHandler.removeCallbacksAndMessages(null);
        setVisibility(View.VISIBLE);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                int progress = progress_in;
                int position = mMyVideoView.getCurrentPosition();
                int duration = getDuration();
                duration = duration > 0 ? duration : 10000000;
                if (progress > 0){
                    position = duration * progress / maxprogress;
                }else {
                    progress = maxprogress * position / duration;
                }
                imageButton_play.setImageResource(mMyVideoView.isPlaying() ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play);
                textview_current.setText(timeFormat(position/1000));
                textview_duration.setText(timeFormat(duration/1000));
                seekbar.setProgress(progress);
                mHandler.removeCallbacks(this);
                mHandler.postDelayed(this, 1000);
            }
        });
        mHandler.postDelayed(mHideRunnable, 5000);
    }

    Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };

    private String timeFormat(int time) {
        int h = time/3600;
        int m = (time-h*3600)/60;
        int s = (time-h*3600) % 60;
        return String.format("%02d:%02d:%02d", h, m, s);
    }

    public void hide() {
        setVisibility(View.GONE);
        mHandler.removeCallbacksAndMessages(null);
    }

    public void startPause(){
        if (mMyVideoView.isPlaying()){
            mMyVideoView.pause();
        }else{
            mMyVideoView.start();
        }
        show();
    }

    int currentPosition = -1;
    public static int maxprogress = 80;
    public void progress(int value){
        hide();
        if (currentPosition == -1){
            currentPosition = mMyVideoView.getCurrentPosition();
            seekbar.setProgress(maxprogress * currentPosition / getDuration());
        }
        int p = value + seekbar.getProgress();
        Timber.d("p: "+p+", value: "+value+", progress: "+ seekbar.getProgress());
        if (p >= 0 && p <= maxprogress){
            seekbar.setProgress(p);
            show(p);
        }else{
            show();
        }
    }

    public void seek(){
        reset();
        mMyVideoView.seekTo(getDuration() * seekbar.getProgress() / maxprogress);
        mMyVideoView.start();
        hide();
    }

    public void reset(){
        currentPosition = -1;
    }
}
